package cn.nbt.busManage.service.impl;

import cn.nbt.busManage.model.dto.PointDto;
import cn.nbt.busManage.model.entity.Point;
import cn.nbt.busManage.dao.repository.PointRepository;
import cn.nbt.busManage.dao.mapper.PointMapper;
import cn.nbt.busManage.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author lyq
 * @time 2024/6/11 17:59
 */

@Service
@Transactional
public class IPointService implements PointService {

    @Autowired
    private PointRepository pointRepository;

    @Autowired
    private PointMapper pointMapper;

    /**
     * 查询所有站点信息
     * @return 所有站点信息
     */
    @Override
    public List<PointDto> findAll() {
        List<Point> points = pointRepository.findAll();
        if(points.isEmpty()) return null;
        ArrayList<PointDto> pointDtos = new ArrayList<>();
        for (Point point : points) {
            PointDto pointDto = new PointDto(point);
            pointDtos.add(pointDto);
        }
        return pointDtos;
    }

    /**
     * 根据信息查询站点
     * @param query 查询信息（名称或id）
     * @return 站点信息
     */
    @Transactional("mybatisTransactionManager")
    @Override
    public List<PointDto> findByQuery(String query) {
        List<Point> points = pointMapper.findByQuery(query);
        if(points.isEmpty()) return null;
        ArrayList<PointDto> pointDtos = new ArrayList<>();
        for (Point point : points) {
            PointDto pointDto = new PointDto(point);
            pointDtos.add(pointDto);
        }
        return pointDtos;
    }

    /**
     * 根据id查询站点
     * @param id 站点id
     * @return 站点信息
     */
    @Override
    public PointDto findById(Integer id) {
        Optional<Point> point = pointRepository.findById(id);
        return point.map(PointDto::new).orElse(null);
    }

    /**
     * 保存站点信息
     * @param pointDto 站点信息
     */
    @Override
    public void save(PointDto pointDto) {
        Point point = new Point();
        point.setId(pointDto.getId());
        point.setPointName(pointDto.getPointName());
        point.setLatitudeLongitude("[" + pointDto.getLatitude() + "," + pointDto.getLongitude() + "]");
        pointRepository.save(point);
    }

    /**
     * 更新站点信息
     * @param pointDto 站点信息
     */
    @Override
    public void update(PointDto pointDto) {
        Point point = new Point();
        point.setId(pointDto.getId());
        point.setPointName(pointDto.getPointName());
        point.setLatitudeLongitude("[" + pointDto.getLatitude() + "," + pointDto.getLongitude() + "]");
        pointRepository.updatePoint(point.getPointName(), point.getLatitudeLongitude(), point.getId());
    }

    /**
     * 删除站点信息
     * @param id 站点id
     */
    @Override
    public void delete(Integer id) {
        pointRepository.deleteById(id);
    }

    /**
     * 根据站点名称查询站点
     * @param pointName 站点名称
     * @return 站点信息
     */
    @Override
    public Point findByPointName(String pointName) {
        return pointRepository.findByPointName(pointName);
    }
}
